package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.exception.ParamException;
import one.stand.model.BusinessCardModel;
import one.stand.model.DynamicModel;
import one.stand.model.ResultModel;
import one.stand.model.essay.EssayVisitorsDataModel;
import one.stand.model.essay.TaskCustomerModel;
import one.stand.model.essay.TaskStaffModel;
import one.stand.request.essay.EssayRecordForm;
import one.stand.request.essay.EssayVisitorsDataForm;
import one.stand.request.essay.EssayVisitorsDataRequest;
import one.stand.request.essay.MyTaskDetailQuery;
import one.stand.response.essay.EssayVisitorsDataResponse;
import one.stand.service.BaseService;
import one.stand.service.IEssayShareRecordService;
import one.stand.service.IEssayVisitorsDataService;
import one.stand.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 获客文章访客数据 服务实现类
 * </p>
 *
 * @author luoyang
 * @since 2020-05-07
 */
@Service
@Transactional
public class EssayVisitorsDataServiceImpl  extends BaseService implements IEssayVisitorsDataService {


    @Autowired
    private IEssayShareRecordService iEssayShareRecordService;


    @Override
    public ResultModel<PageInfo<EssayVisitorsDataResponse>> queryVisitorsData(EssayVisitorsDataRequest request) {
        if(request.getCompanyId() == null){
            request.setCompanyId(request.getCId());
        }

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        log.info("查询访客数据: {}", JSON.toJSONString(request));
        List<EssayVisitorsDataResponse> list = this.essayVisitorsDataMapper.queryVisitorsData(request);

        PageInfo<EssayVisitorsDataResponse> pageInfo = new PageInfo(list);
        return ResultModel.success(pageInfo);

    }

    /**
     * 添加更新访客数据。每次都新增 如果存在则增加次数 和 总时长
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResultModel<Boolean> addVisitorsData(EssayVisitorsDataForm request) throws ParamException {
        log.info("addVisitorsData:",request);
        EssayVisitorsDataModel model = this.essayVisitorsDataMapper.queryVisitors(request);
        if(model == null){
//            model = new EssayVisitorsDataModel();
//            BeanUtils.copyProperties(request,model);
//            model.setCreatedTime(DateUtil.now());
//            model.setVisitTime(DateUtil.now());
//            model.setForwardfNum(0);
//            model = countModel(model,request);
//            if(this.essayVisitorsDataMapper.insert(model) > 0){
//                //更新分享浏览次数和获客数
//                addShareRecord(request,2);
//                return ResultModel.success();
//            }
            log.info("访客数据异常:{}",request);
            return ResultModel.fail();
        }else {
            model.setSumTimes(model.getSumTimes() == null ? request.getSumTimes() : request.getSumTimes() + model.getSumTimes());
            //添加新的
            if(this.essayVisitorsDataMapper.update(model) > 0){
                //更新分享浏览次数
//                addShareRecord(request,3);
                return ResultModel.success();
            }
        }
        return ResultModel.fail("数据错误");
    }

    private void updateVisitNum(EssayVisitorsDataModel model){
        //更新员工任务分享数据
        TaskStaffModel taskStaffModel = this.taskStaffMapper.queryMyTaskStaffDetail(new MyTaskDetailQuery(){{
            setUserId(model.getUserId());
            setDynamicId(model.getDynamicId());
        }});

        if(taskStaffModel != null){
            taskStaffModel.setVisitNum(taskStaffModel.getVisitNum() == null ? 1 : taskStaffModel.getVisitNum()+1);
            this.taskStaffMapper.update(taskStaffModel);
            //检查如果分享次数和达到任务次数则已经完成
            TaskCustomerModel taskCustomerModel = this.taskCustomerMapper.queryById(taskStaffModel.getTaskId().intValue());
            if(taskCustomerModel != null){
                if(taskStaffModel.getVisitNum() .intValue() == taskCustomerModel.getVisitNum().intValue() && taskStaffModel.getShareNum().intValue() == taskCustomerModel.getShareNum().intValue()){
                    taskStaffModel.setState(1);
                    this.taskStaffMapper.update(taskStaffModel);
                }
            }else{
                log.info("TaskCustomerModel_error:{}",taskStaffModel.getTaskId());
            }
        }else{
            log.info("更新员工任务分享数据异常:{}",model);
        }
    }

    /**
     * 按文章 业务员 公司查询记录 如果没有则添加 有则更新
     * @param request
     * @return
     */
//    private boolean addShareRecord(EssayVisitorsDataForm request,int type) throws ParamException {
//        EssayRecordForm dataForm = new EssayRecordForm();
//        BeanUtils.copyProperties(request,dataForm);
//        dataForm.setType(type);//浏览次数
//        //通过cardid查找用户id
//        BusinessCardModel businessCardModel = this.businessCardMapper.selectByPrimaryKey(request.getCardId());
//        if(businessCardModel == null){
//            throw new ParamException("参数缺失");
//        }
//        dataForm.setUserId(Long.valueOf(businessCardModel.getUserId()));
//
//        return iEssayShareRecordService.addShareRecordData(dataForm).isSuccess() ;
//    }

//    /**
//     * 第一次访客数据
//     * @param model
//     * @param request
//     * @return
//     */
//    private EssayVisitorsDataModel countModel(EssayVisitorsDataModel model,EssayVisitorsDataForm request){
//        switch (request.getType()){
////            操作类型:1总次数 总时长 2 转发次数
//            case 1 :
//                model.setSumNum(model.getSumNum() == null ? 1 : model.getSumNum() + 1);
//                model.setSumTimes(model.getSumTimes() == null ? 0 : model.getSumTimes() + (request.getSumTimes() == null ? 0 : request.getSumTimes()) );
//                break;
//            case 2:
//                model.setForwardfNum(model.getForwardfNum() == null ? 1 : model.getForwardfNum() + 1);
//                break;
//        }
//        return model;
//    }


//    private EssayVisitorsDataModel countModelNext(EssayVisitorsDataModel model,EssayVisitorsDataForm request,EssayVisitorsDataModel newModel){
//        switch (request.getType()){
////            操作类型:1总次数 总时长 2 转发次数
//            case 1 :
//                newModel.setSumNum(model.getSumNum() == null ? 1 : model.getSumNum() + 1);
//                newModel.setSumTimes(model.getSumTimes() == null ? 0 : model.getSumTimes() + (request.getSumTimes() == null ? 0 : request.getSumTimes()) );
//                break;
//            case 2:
//                newModel.setForwardfNum(model.getForwardfNum() == null ? 1 : model.getForwardfNum() + 1);
//                break;
//        }
//        return model;
//    }

    private void updateDynamicModel(EssayVisitorsDataForm request){
        //更新文章分享次数
        DynamicModel dynamicModel = this.dynamicMapper.selectByPrimaryKey(request.getDynamicId().intValue());
        if(dynamicModel == null){
            //throw new Exception
//            return ResultModel.fail("文章错误");
        }else {
//            操作类型:1总次数 总时长 2 转发次数
//            switch (request.getType()){
//                case 1:
//                    dynamicModel.setReadTime(dynamicModel.getReadTime() + (request.getSumTimes() == null ? 0 : request.getSumTimes()));
//                    dynamicModel.setReadNum(dynamicModel.getReadNum() + 1);
//            }

            this.dynamicMapper.updateByPrimaryKeySelective(dynamicModel);
        }
    }

}
